package cn.web.tunnelrobot.service.impl;
import cn.web.tunnelrobot.dao.ProjectDao;
import cn.web.tunnelrobot.dao.*;
import cn.web.tunnelrobot.dto.HardwareTestInfoDTO;
import cn.web.tunnelrobot.dto.Warning;
import cn.web.tunnelrobot.dto.WarningRecordDTO;
import cn.web.tunnelrobot.pojo.Project;
import cn.web.tunnelrobot.service.HardwareTestInfoService;
import cn.web.tunnelrobot.util.Calculate;
import cn.web.tunnelrobot.util.Constants;
import cn.web.tunnelrobot.util.MileageConversion;
import cn.web.tunnelrobot.websocket.ServerThread;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 硬件检测数据Service层实现类
 */
@Service
public class HardwareTestInfoServiceImpl implements HardwareTestInfoService {
    @Autowired
    private HardwareTestInfoDao hardwareTestInfoDao;
    @Autowired
    private UserRecordDao userRecordDao;
    @Autowired
    private WarningRecordDao warningRecordDao;
    @Autowired
    private ThresholdDao thresholdDao;

    @Autowired
    private ProjectDao projectDao;
    //获取项目信息
    public static Project project = new Project();


//    //小数点转换工具==累计沉降\收敛
//    DecimalFormat decimalFormat = new DecimalFormat("0.00");
//    //获取用户session信息
//    //HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//    //User user = (User) request.getSession().getAttribute(Constants.USER_SESSION);
    @Override
    public HashMap<String,Object> getHardwareTestInfoByPrIdAndMileage(Integer prId, String htTurnMeter1, Integer pageNo, Integer pageSize) {
        HashMap<String,Object> map = new HashMap<>();
        try {
            if(htTurnMeter1!=null){
                int distance1 = MileageConversion.distance(htTurnMeter1);
                String distance2 = String.valueOf(distance1);
                List<HardwareTestInfoDTO> hardwareTestInfoList = hardwareTestInfoDao.getHardwareTestInfoByPrIdAndMileage(prId,distance2,(pageNo-1)*pageSize,pageSize);
                if(hardwareTestInfoList.size() > 0) {
                    for (int i = 0; i < hardwareTestInfoList.size(); i++) {
                        Float htTurnMeter2 = hardwareTestInfoList.get(i).getHtTurnMeter1();
                        if (htTurnMeter2 == null) {
                            htTurnMeter2 = (float) 0;
                        }
                        String DK = MileageConversion.mileage(htTurnMeter2);
                        hardwareTestInfoList.get(i).setHtMileageDK(DK);
                    }
                }
                int count = hardwareTestInfoDao.getHardwareTestInfoByPrIdAndMileageCount(prId, distance2);
                map.put("data",hardwareTestInfoList);
                map.put("count",count);
                map.put("code",0);
                return map;
            }else{
                List<HardwareTestInfoDTO> hardwareTestInfoList = hardwareTestInfoDao.getHardwareTestInfoByPrIdAndMileage(prId,null,(pageNo-1)*pageSize,pageSize);
                if(hardwareTestInfoList.size() > 0) {
                    for (int i = 0; i < hardwareTestInfoList.size(); i++) {
                        Float htTurnMeter2 = hardwareTestInfoList.get(i).getHtTurnMeter1();
                        if (htTurnMeter2 == null) {
                            htTurnMeter2 = (float) 0;
                        }
                        String DK = MileageConversion.mileage(htTurnMeter2);
                        hardwareTestInfoList.get(i).setHtMileageDK(DK);
                    }
                }
                map.put("data",hardwareTestInfoList);
                int count = hardwareTestInfoDao.getHardwareTestInfoByPrIdAndMileageCount(prId, null);
                map.put("count",count);
                map.put("code",0);
                return map;
            }

        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","信息查询时出现问题");
            map.put("code",-1);
            return map;
        }
    }
    @Override
    public HashMap<String,Object> getHardwareTestInfoByRange(Integer prId,Integer beginHtMileage,Integer endHtMileage,Integer pageNo,Integer pageSize){
        HashMap<String,Object> map = new HashMap<>();
        try {
            List<HardwareTestInfoDTO> hardwareTestInfoList = hardwareTestInfoDao.getHardwareTestInfoByRange(prId,beginHtMileage,endHtMileage,(pageNo-1)*pageSize,pageSize);
            /*UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user.getUsId());
            userRecord.setUrOperatContent("查询报表列表信息");
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
            if(hardwareTestInfoList.size() > 0){
                for(int i=0;i<hardwareTestInfoList.size();i++){
                    String DK=MileageConversion.mileage(hardwareTestInfoList.get(i).getHtTurnMeter1());
                    hardwareTestInfoList.get(i).setHtMileageDK(DK);
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String dateString = formatter.format(hardwareTestInfoList.get(i).getHtCreateDate());
                    hardwareTestInfoList.get(i).setHtCreateDateFormat(dateString);

                }
                //整个隧道只有一条中轴线    轮廓线在每个点位中具有数据
                Random random = new Random();
                int i = random.nextInt(4);
                String url = "/uploadfiles/"+i+".png";
                map.put("axleWire",url);//中轴线
                map.put("contourLine",url);//轮廓线
                map.put("data",hardwareTestInfoList);
                int count = hardwareTestInfoDao.getHardwareTestInfoByRangeCount(prId, beginHtMileage,endHtMileage);
                map.put("count",count);
/*                //cumulativeSettl   累计沉降值   cumulativeConvergence  累计收敛值
                HardwareTestInfoDTO hardwareTestInfoData = hardwareTestInfoDao.getHardwareTestInfoData(prId,htMileage);
                map.put("cumulativeSettl",decimalFormat.format(hardwareTestInfoData.getHtDailySettl()));
                map.put("cumulativeConvergence",decimalFormat.format(hardwareTestInfoData.getHtDailyConvergence()));*/
                map.put("code",0);
                return map;
            }else{
                map.put("data","查询暂无信息");
                map.put("code",-1);
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","信息查询时出现问题");
            map.put("code",-1);
            return map;
        }
    }

    //激光模块采集数据
    @Override
    public HashMap<String, Object> addHardwareTestInfoDTO(Integer whichLaserModule, Integer htAngle, Integer collectWay) {
        HashMap<String,Object> map = new HashMap<>();
        if(Constants.is_intranet == 1){
            map.put("code",-1);
            map.put("msg","外网没有权限");
            return map;
        }
        //获取云台旋转角度上下限值
        Constants.cloudTurnAngle = thresholdDao.getThresholdListById(39);
        try {
            //采集方式    1有线  2无线
            if(collectWay == 1){
                //发送指令获取数据
                //获取雷达硬件采集数据-先从正数角度获取，再从负数角度获取  最后获取0°的信息 -15L 15R
                for(Integer i = htAngle;i <= Constants.cloudTurnAngle.getThPrewarningRightValue();i=i+htAngle){
                    Double htAngle1 = i - 0.3;
                    Double htAngle2 = i + 0.3;
                    //发送指令获取数据
                    String result = Constants.doGet(Constants.cloudDiskControl+ "?module="+whichLaserModule+"&angle="+htAngle);
                    if(StringUtils.isNotEmpty(result)){
//                        int returnAngle1 = Integer.parseInt(result);
                        Double returnAngle1 = Double.parseDouble(result);
                        //硬件返回的角度 误差上下限 0.3
                        if(returnAngle1 >= htAngle1 && returnAngle1 <= htAngle2){
//                        if(result.equals(htAngle.toString())){
                            //采集信息
                            String result1 = Constants.doGet(Constants.metricalData+ "?module="+whichLaserModule+"&flag="+1);
                            result1 = result.substring(1,result1.length()-1);
                            List<String> list1 = Arrays.asList(result1.split(","));
                            int location = Integer.parseInt(list1.get(0));
                            Double height = Double.parseDouble(list1.get(1));
                            Double width = Double.parseDouble(list1.get(2));
                            if (height < 30 && width < 30){
                                collectInformation(i, width, height,location);
                            }
//                            collectInformation(htAngle, width, height,location);
                        }
                    }
                }
                for(Integer i = -htAngle;i >= Constants.cloudTurnAngle.getThPrewarningLeftValue();i=i-htAngle){
                    Double htAngle1 = i - 0.3;
                    Double htAngle2 = i + 0.3;
                    //发送指令获取数据
                    String result = Constants.doGet(Constants.cloudDiskControl+ "?module="+whichLaserModule+"&angle="+htAngle);
                    if(StringUtils.isNotEmpty(result)){
                        Double returnAngle1 = Double.parseDouble(result);
                        //硬件返回的角度 误差上下限 0.3
                        if(returnAngle1 >= htAngle1 && returnAngle1 <= htAngle2){
//                        if(result.equals(htAngle.toString())){
                            //采集信息
                            String result1 = Constants.doGet(Constants.metricalData+ "?module="+whichLaserModule+"&flag="+1);
                            if(StringUtils.isNotEmpty(result1)){
                                result1 = result.substring(1,result1.length()-1);
                                List<String> list1 = Arrays.asList(result1.split(","));
                                int location = Integer.parseInt(list1.get(0));
                                Double height = Double.parseDouble(list1.get(1));
                                Double width = Double.parseDouble(list1.get(2));
                                if (height < 30 && width < 30){
                                    collectInformation(htAngle, width, height,location);
                                }
//                                collectInformation(htAngle, width, height,location);
                            }
                        }
                    }
                }
                //复位0°----采集信息
                //发送指令获取数据
                String result = Constants.doGet(Constants.cloudDiskControl+ "?module="+whichLaserModule+"&angle="+0);
                if(StringUtils.isNotEmpty(result)){
//                    if(result.equals(htAngle.toString())){
                    Double returnAngle1 = Double.parseDouble(result);
                    //硬件返回的角度 误差上下限 0.3
                    if(returnAngle1 >= -0.3 && returnAngle1 <= 0.3){
                        //采集信息
                        String result1 = Constants.doGet(Constants.metricalData+ "?module="+whichLaserModule+"&flag="+1);
                        if(StringUtils.isNotEmpty(result1)){
                            result1 = result.substring(1,result1.length()-1);
                            List<String> list1 = Arrays.asList(result1.split(","));
                            int location = Integer.parseInt(list1.get(0));
                            Double height = Double.parseDouble(list1.get(1));
                            Double width = Double.parseDouble(list1.get(2));
                            if (height < 30 && width < 30){
                                collectInformation(0, width, height,location);
                            }
                        }
                    }
                }
            }else if(collectWay == 2){
                //无线
                //获取雷达硬件采集数据-先从正数角度获取，再从负数角度获取  最后获取0°的信息


                //循环遍历请求的角度 小于云台旋转角度上限值
                for(Integer i = htAngle;i <= Constants.cloudTurnAngle.getThPrewarningLeftValue();i=i+htAngle){
                    Double htAngle1 = i - 0.3;
                    Double htAngle2 = i + 0.3;

                    //System.out.println("$SREVO:"+whichLaserModule+","+i+"!");
                    //发送云盘指令 并接收返回值
                    String returnAngle = ServerThread.run("$SREVO:"+whichLaserModule+","+i+"!");//云盘控制指令
                    //System.out.println("1:"+returnAngle);
                    if(StringUtils.isNotEmpty(returnAngle) && returnAngle.indexOf("SREV")!=-1){
                        returnAngle = returnAngle.substring(returnAngle.indexOf(":")+1,returnAngle.indexOf("@"));
                        //System.out.println("2:"+returnAngle);
                        Double returnAngle1 = Double.parseDouble(returnAngle);
//                        System.out.println("3:"+returnAngle1);
//                        System.out.println("htAngle1:"+htAngle1);
//                        System.out.println("htAngle2:"+htAngle2);
                        //硬件返回的角度 误差上下限 0.3
                        if(returnAngle1 >= htAngle1 && returnAngle1 <= htAngle2){
                            //采集信息
                            String returnInfo = ServerThread.run("$Measure:"+whichLaserModule+",1!");
//                            System.out.println("11:"+returnInfo);
                            //判断是否有遮挡
//                            if(StringUtils.isNotEmpty(returnInfo)){
                            if(StringUtils.isNotEmpty(returnInfo) && returnInfo.indexOf("C")!=-1){
                                //云台的ID号 根据ID号来算出云台在隧道的位置
                                int location = Integer.parseInt(returnInfo.substring(returnInfo.indexOf("C")+1,returnInfo.indexOf("H")));
                                String heightStr = returnInfo.substring(returnInfo.indexOf("H") + 1, returnInfo.indexOf("W"));
                                String widthStr = returnInfo.substring(returnInfo.indexOf("W") + 1, returnInfo.indexOf("@"));
                                //返回信息中包含ERR---15说明测量时有遮挡
//                                if (!heightStr.equals("ERR") && !widthStr.equals("ERR")){
                                if (returnInfo.indexOf("ERR")==-1){
                                    Double height = Double.parseDouble(heightStr);
                                    Double width = Double.parseDouble(widthStr);
//                                    System.out.println("高:"+height);
//                                    System.out.println("宽:"+width);
//                                    System.out.println("位置:"+location);
//                                    System.out.println("角度:"+i);
                                    //如果测的高度和宽度过大，误差会很大 舍弃
                                    if (height < 30 && width < 30){
                                        collectInformation(i, width, height,location);
                                    }
                                }
                            }
                        }
                    }
                }


                //循环遍历请求的角度 大于云台旋转角度下限值
                for(Integer i = -htAngle;i >=Constants.cloudTurnAngle.getThPrewarningRightValue();i=i-htAngle){
                    Double htAngle1 = i - 0.3;
                    Double htAngle2 = i + 0.3;
//                    System.out.println("i:"+i);
//                    System.out.println("$SREVO:"+whichLaserModule+","+i+"!");
                    String returnAngle = ServerThread.run("$SREVO:"+whichLaserModule+","+i+"!");//云盘控制指令
//                    System.out.println("4:"+returnAngle);
                    if(StringUtils.isNotEmpty(returnAngle) && returnAngle.indexOf("SREV")!=-1){
                        returnAngle = returnAngle.substring(returnAngle.indexOf(":")+1,returnAngle.indexOf("@"));
//                        System.out.println("5:"+returnAngle);
                        Double returnAngle1 = Double.parseDouble(returnAngle);
//                        System.out.println("6:"+returnAngle1);
                        //硬件返回的角度 误差上下限 0.3
                        if(returnAngle1 >= htAngle1 && returnAngle1 <= htAngle2){
                            //采集信息
                            String returnInfo = ServerThread.run("$Measure:"+whichLaserModule+",1!");//云盘控制指令
//                            System.out.println("-11:"+returnInfo);
                            //判断是否有遮挡
                            if(StringUtils.isNotEmpty(returnInfo) && returnInfo.indexOf("C")!=-1){
                                int location = Integer.parseInt(returnInfo.substring(returnInfo.indexOf("C")+1,returnInfo.indexOf("H")));
                                String heightStr = returnInfo.substring(returnInfo.indexOf("H") + 1, returnInfo.indexOf("W"));
                                String widthStr = returnInfo.substring(returnInfo.indexOf("W") + 1, returnInfo.indexOf("@"));
//                                //判断是否包含错误
//                                if (!heightStr.equals("ERR") && !widthStr.equals("ERR")){
//                                if (heightStr.indexOf("ERR")==-1 && widthStr.indexOf("ERR")==-1){
                                if (heightStr.indexOf("ERR")==-1 && widthStr.indexOf("ERR")==-1){
                                    Double height = Double.parseDouble(heightStr);
                                    Double width = Double.parseDouble(widthStr);
//                                    System.out.println("位置:"+location);
//                                    System.out.println("高:"+height);
//                                    System.out.println("宽:"+width);
//                                    System.out.println("角度:"+i);
                                    if (height < 30 && width < 30){
                                        collectInformation(i, width, height,location);
                                    }
                                }
                            }
                        }
                    }
                }



                //复位0°----采集信息
                String returnAngle = ServerThread.run("$SREVO:"+whichLaserModule+","+0+"!");//云盘控制指令
                if(StringUtils.isNotEmpty(returnAngle) && returnAngle.indexOf("SREV")!=-1){
                    returnAngle = returnAngle.substring(returnAngle.indexOf(":")+1,returnAngle.indexOf("@"));
                    Double returnAngle1 = Double.parseDouble(returnAngle);
                    //硬件返回的角度 误差上下限 0.3 改成0.5
                    if(returnAngle1 >= -0.5 && returnAngle1 <= 0.5){
                        //采集信息
                        String returnInfo = ServerThread.run("$Measure:"+whichLaserModule+",1!");//云盘控制指令
//                        System.out.println("0:"+returnInfo);
                        if(StringUtils.isNotEmpty(returnInfo) && returnInfo.indexOf("C")!=-1){ //if(!returnInfo.substring(0,6).equals("Error!")){//判断是否有遮挡}
                            //获取原始位置 也就是获取沉降收敛的设备号 根据激光模块间隔多少米来算出具体所处位置
                            int location = Integer.parseInt(returnInfo.substring(returnInfo.indexOf("C")+1,returnInfo.indexOf("H")));
                            String heightStr = returnInfo.substring(returnInfo.indexOf("H") + 1, returnInfo.indexOf("W"));
                            String widthStr = returnInfo.substring(returnInfo.indexOf("W") + 1, returnInfo.indexOf("@"));
//                            if (!heightStr.equals("ERR") && !widthStr.equals("ERR")){
//                            if (heightStr.indexOf("ERR")==-1 && widthStr.indexOf("ERR")==-1){
                            if (heightStr.indexOf("ERR")==-1 && widthStr.indexOf("ERR")==-1){
                                Double height = Double.parseDouble(heightStr);
                                Double width = Double.parseDouble(widthStr);
//                                System.out.println("位置:"+location);
//                                System.out.println("高:"+height);
//                                System.out.println("宽:"+width);
                                if (height < 30 && width < 30){
                                    collectInformation(0, width, height,location);
                                }
                            }
                        }
                    }
                }
            }
            map.put("data","采集成功");
            map.put("code",0);
            return map;
        } catch (Exception e) {
            //e.printStackTrace();
            map.put("data","采集失败");
            map.put("code",-1);
            return map;
        }
    }
    //测试测试
    @Override
    public HashMap<String, Object> testHardware(Integer whichLaserModule, Integer htAngle, Integer collectWay){
        HashMap<String,Object> map = new HashMap<>();
        try{
            //for(Integer i = htAngle;i <= 0;i=i+htAngle){
                Integer i = htAngle;
                Double htAngle1 = i - 0.3;
                Double htAngle2 = i + 0.3;
                System.out.println("i:"+i);
                System.out.println("$SREVO:"+whichLaserModule+","+i+"!");
                //发送云盘指令 并接收返回值
                String returnAngle = ServerThread.run("$SREVO:"+whichLaserModule+","+i+"!");//云盘控制指令
                System.out.println("1:"+returnAngle);
                if(StringUtils.isNotEmpty(returnAngle) && returnAngle.indexOf("SREV")!=-1){
                    returnAngle = returnAngle.substring(returnAngle.indexOf(":")+1,returnAngle.indexOf("@"));
                    System.out.println("2:"+returnAngle);
                    Double returnAngle1 = Double.parseDouble(returnAngle);
                    System.out.println("3:"+returnAngle1);
                    System.out.println("htAngle1:"+htAngle1);
                    System.out.println("htAngle2:"+htAngle2);
                    //硬件返回的角度 误差上下限 0.3
                    if(returnAngle1 >= htAngle1 && returnAngle1 <= htAngle2){
                        //采集信息
                        String returnInfo = ServerThread.run("$Measure:"+whichLaserModule+",1!");
                        System.out.println("11:"+returnInfo);
                        //判断是否有遮挡
                        if(StringUtils.isNotEmpty(returnInfo)){
                            //云台的ID号 根据ID号来算出云台在隧道的位置
                            int location = Integer.parseInt(returnInfo.substring(returnInfo.indexOf("C")+1,returnInfo.indexOf("H")));
                            String heightStr = returnInfo.substring(returnInfo.indexOf("H") + 1, returnInfo.indexOf("W"));
                            String widthStr = returnInfo.substring(returnInfo.indexOf("W") + 1, returnInfo.indexOf("@"));
                            //返回信息中包含ERR 说明有遮挡 当两者都没遮挡进行下一步的操作
                            if (heightStr.indexOf("ERR")==-1 && widthStr.indexOf("ERR")==-1){
                                Double height = Double.parseDouble(heightStr);
                                Double width = Double.parseDouble(widthStr);
                                System.out.println("高:"+height);
                                System.out.println("宽:"+width);
                                System.out.println("位置:"+location);
                                System.out.println("角度:"+i);
                                //如果测的高度和宽度过大，误差会很大 舍弃
                                if (height < 30 && width < 30){
                                    collectInformation(i, width, height,location);
                                }
                            }
                            else{
                                System.out.println("出现遮挡，不进行计算");
                            }
                        }
                    }
                }
            //}
            map.put("data","采集成功");
            map.put("code",0);
            return map;
        }catch (Exception e) {
            e.printStackTrace();
            map.put("data","采集失败");
            map.put("code",-1);
            return map;
        }



    }

    /**
     * 计算沉降、收敛值  及其  位置
     * @param htAngle 角度
     * @param width 宽
     * @param height 高
     * @param location 原始位置
     */
    public void collectInformation(Integer htAngle, Double width, Double height,Integer location){
        WarningRecordDTO warningRecordDTO = new WarningRecordDTO();
        //二版激光模块所在位置=当前项目隧道初始位置+当前激光模块ID*激光模块间隔多少米放置一个 数据库暂定为100m放置一个 例如：1000+1*100=30 云台在隧道1030米处
        //获取项目信息
        project=projectDao.getProjectById(1);
        //当前项目的隧道初始位置，
        Integer projectInitialLocation=Integer.valueOf((int) project.getInitial().floatValue());
        // 计算位置  project.getPrLaserModule()为激光模块间隔多少米放置一个
        location=projectInitialLocation+location*project.getPrLaserModule();

        /**沉降*/
        //在location位置上转动htAngle角度后的沉降位置 精确到小数点后两位
        Float settLocation =Calculate.settValue(location,height,htAngle);
        //沉降值
        Float settValue = Calculate.settValue(height);
        //根据位置、角度、数据类型获取历史数据。如果为空就进行数据库插入，如果不为空进行数据的更新，总之保证数据库 某个里程只有一条数据
        HardwareTestInfoDTO getLastHardwareTestInfoDTO = hardwareTestInfoDao.getLastHardwareTestInfoDTO(location,Float.parseFloat(String.valueOf(htAngle)),0);
//        System.out.println("沉降值："+hardwareTestInfoDTO.getHtCumulativeDailySettl());
        if(getLastHardwareTestInfoDTO != null){

            //dailySettl 每次沉降 cumulativeDailySettl 沉降差  cumulativeSettl 累计沉降
            //最后一次沉降值
            Float lastSettValue=getLastHardwareTestInfoDTO.getHtDailySettl();
            getLastHardwareTestInfoDTO.setHtDailySettl(settValue);
            // 当前位置当日沉降值 当前沉降值-最后一次沉降值
            Float dailySettl=Calculate.ToFloat01(settValue-lastSettValue);
//            System.out.println(htAngle+"度当日真实累计沉降值"+dailySettl);
            if((dailySettl>=(-50)&&dailySettl<=(-2))||(dailySettl>=2&&dailySettl<=50)){
//                System.out.println(htAngle+"度当日累计沉降值"+dailySettl+" 进行减少误差");
                if(dailySettl>0){
                    dailySettl=Calculate.ToFloat01((float) 0.2);
                }else{
                    dailySettl=Calculate.ToFloat01((float) -0.2);
                }

            }
            getLastHardwareTestInfoDTO.setHtCumulativeDailySettl(dailySettl);
            Float lastCumulativeSettl=getLastHardwareTestInfoDTO.getHtCumulativeSettl();
            //当前位置累计沉降值
            Float cumulativeSettl=Calculate.ToFloat01(dailySettl+lastCumulativeSettl);
//            System.out.println(htAngle+"度累计沉降值"+cumulativeSettl);
            getLastHardwareTestInfoDTO.setHtCumulativeSettl(cumulativeSettl);
            getLastHardwareTestInfoDTO.setHtTurnMeter1(settLocation);
            //日期
            getLastHardwareTestInfoDTO.setHtCreateDate(new Date());
            //数据库更新
            hardwareTestInfoDao.updateHardwareTestInfoDTO(getLastHardwareTestInfoDTO);
        }else{

            getLastHardwareTestInfoDTO = new HardwareTestInfoDTO();
            //当前位置所属项目id
            getLastHardwareTestInfoDTO.setPrId(1);
            //位置（里程）
            getLastHardwareTestInfoDTO.setHtMileage(location);
            //转动角度
            getLastHardwareTestInfoDTO.setHtAngle(Float.parseFloat(String.valueOf(htAngle)));
            //当前位置最后一次沉降值
            getLastHardwareTestInfoDTO.setHtDailySettl(settValue);
            if(htAngle==0){
                //沉降值
                Float convergenceValue01 = Calculate.convergenceValue(width,htAngle);
                getLastHardwareTestInfoDTO.setHtDailyConvergence(convergenceValue01);
                getLastHardwareTestInfoDTO.setHtCumulativeDailyConvergence(0f);
                getLastHardwareTestInfoDTO.setHtCumulativeConvergence(0f);
            }
            //每日累计沉降值
            getLastHardwareTestInfoDTO.setHtCumulativeDailySettl(0f);
            //总沉降值
            getLastHardwareTestInfoDTO.setHtCumulativeSettl(0f);
            //转动米数（里程数）
            getLastHardwareTestInfoDTO.setHtTurnMeter1(settLocation);
            //日期
            getLastHardwareTestInfoDTO.setHtCreateDate(new Date());
            //沉降值
            getLastHardwareTestInfoDTO.setHtInfoWarn(1);
            //转动米数
            getLastHardwareTestInfoDTO.setHtTurnMeter(settLocation);
            //添加数据
            hardwareTestInfoDao.addHardwareTestInfoDTO(getLastHardwareTestInfoDTO);
        }


        /**收敛*/

        //后沉降位置: 在location位置上转动htAngle角度 精确到小数点后两位
        Float convergenceLocation =Calculate.convergenceValue(location,width,htAngle);
        //沉降值
        Float convergenceValue = Calculate.convergenceValue(width,htAngle);
        //获取历史数据
        getLastHardwareTestInfoDTO = hardwareTestInfoDao.getLastHardwareTestInfoDTO(location,Float.parseFloat(String.valueOf(htAngle)),1);
        if(getLastHardwareTestInfoDTO != null){
            //dailySettl 每次沉降 cumulativeDailySettl 沉降差  cumulativeSettl 累计沉降
            //最后一次沉降值
            Float lastConvergenceValue=getLastHardwareTestInfoDTO.getHtDailyConvergence();
            getLastHardwareTestInfoDTO.setHtDailyConvergence(convergenceValue);
            // 当前位置当日沉降值 当前沉降值-最后一次沉降值
            Float dailyConvergence=Calculate.ToFloat01(convergenceValue-lastConvergenceValue);
//            System.out.println(htAngle+"度当日真实累计收敛值"+dailyConvergence);
            if((dailyConvergence>=(-50)&&dailyConvergence<=(-2))||(dailyConvergence>=2&&dailyConvergence<=50)){
                //如果沉降在3mm之内 则认为是误差 进行矫正
//                System.out.println(htAngle+"度当日累计收敛值"+dailyConvergence+" 进行减少误差");
                if(dailyConvergence>0){
                    dailyConvergence=Calculate.ToFloat01((float) 0.2);
                }else{
                    dailyConvergence=Calculate.ToFloat01((float) -0.2);
                }
            }
            getLastHardwareTestInfoDTO.setHtCumulativeDailyConvergence(dailyConvergence);
            Float lastCumulativeConvergenceValue=getLastHardwareTestInfoDTO.getHtCumulativeConvergence();
            Float cumulativeConvergence=Calculate.ToFloat01(dailyConvergence+lastCumulativeConvergenceValue);
            getLastHardwareTestInfoDTO.setHtCumulativeConvergence(cumulativeConvergence);
//            System.out.println(htAngle+"度累计收敛值："+cumulativeConvergence);
            getLastHardwareTestInfoDTO.setHtInfoWarn(2);
            getLastHardwareTestInfoDTO.setHtTurnMeter1(convergenceLocation);
            getLastHardwareTestInfoDTO.setHtCreateDate(new Date());
            hardwareTestInfoDao.updateHardwareTestInfoDTO(getLastHardwareTestInfoDTO);

        }else{
            getLastHardwareTestInfoDTO = new HardwareTestInfoDTO();
            //dailySettl 每次沉降 cumulativeDailySettl 沉降差  cumulativeSettl 累计沉降
            //当前位置所属项目id
            getLastHardwareTestInfoDTO.setPrId(1);
            //位置（里程）
            getLastHardwareTestInfoDTO.setHtMileage(location);
            //转动角度
            getLastHardwareTestInfoDTO.setHtAngle(Float.parseFloat(String.valueOf(htAngle)));
            //当前位置最后一次沉降值
            getLastHardwareTestInfoDTO.setHtDailyConvergence(convergenceValue);
            //每日累计沉降值
            getLastHardwareTestInfoDTO.setHtCumulativeDailyConvergence(0f);
            //总沉降值
            getLastHardwareTestInfoDTO.setHtCumulativeConvergence(0f);
            //转动米数（里程数）
            getLastHardwareTestInfoDTO.setHtTurnMeter1(convergenceLocation);
            //日期
            getLastHardwareTestInfoDTO.setHtCreateDate(new Date());
            //沉降值
            getLastHardwareTestInfoDTO.setHtInfoWarn(2);
            //转动米数
            getLastHardwareTestInfoDTO.setHtTurnMeter(convergenceLocation);
            //添加数据
            hardwareTestInfoDao.addHardwareTestInfoDTO(getLastHardwareTestInfoDTO);
        }

        HardwareTestInfoDTO hardwareTestInfoDTO = new HardwareTestInfoDTO();

        //判断沉降是否过阈值设置
        float cumulativeDailySettlSum = hardwareTestInfoDao.getLastHardwareTestInfoDTOSum1(location,Float.parseFloat(String.valueOf(htAngle)),0);
//        System.out.println("累计沉降："+cumulativeDailySettlSum);
//        System.out.println("小于："+Constants.settle.getThPrewarningRightValue());
//        System.out.println("大于："+Constants.settle.getThPrewarningLeftValue());
        //<(-100) 或 >100 下降超过100毫米 或 上升超过100毫米进行报警
        if(cumulativeDailySettlSum <  Constants.settle.getThPrewarningRightValue() || cumulativeDailySettlSum >  Constants.settle.getThPrewarningLeftValue()){
            warningRecordDTO.setWrLevel(Constants.settle.getThWarningLevel());
            warningRecordDTO.setWrDescribe(Constants.settle.getThWarningDescribe());
            warningRecordDTO.setWrWarningTime(new Date());
            warningRecordDao.addWarningRecordInfo(warningRecordDTO);
            //System.out.println("累计沉降ID:"+ warningRecordDTO.getWrId());
            Warning warning = new Warning();
            warning.setId(warningRecordDTO.getWrId());
            warning.setCode(8);
            warning.setLevel(Constants.settle.getThWarningLevel());
            warning.setMsg(Constants.settle.getThWarningDescribe());
            Constants.warningList.add(warning);
//            StartService.timer();
        }
        //判断收敛是否过阈值设置
        float cumulativeDailySettlSum1 = hardwareTestInfoDao.getLastHardwareTestInfoDTOSum2(location,Float.parseFloat(String.valueOf(htAngle)),1);
//        System.out.println("累计收敛："+cumulativeDailySettlSum1);
//        System.out.println("小于："+Constants.convergence.getThPrewarningRightValue());
//        System.out.println("大于："+Constants.convergence.getThPrewarningLeftValue());
        //<(-100) 或 >100 变窄超过100毫米 或 变宽超过100毫米进行报警
        if(cumulativeDailySettlSum1 <  Constants.convergence.getThPrewarningRightValue() || cumulativeDailySettlSum1 > Constants.convergence.getThPrewarningLeftValue()){
            warningRecordDTO.setWrLevel(Constants.convergence.getThWarningLevel());
            warningRecordDTO.setWrDescribe(Constants.convergence.getThWarningDescribe());
            warningRecordDTO.setWrWarningTime(new Date());
            warningRecordDao.addWarningRecordInfo(warningRecordDTO);
            //System.out.println("累计沉降ID:"+ warningRecordDTO.getWrId());
            Warning warning = new Warning();
            warning.setId(warningRecordDTO.getWrId());
            warning.setCode(8);
            warning.setLevel(Constants.convergence.getThWarningLevel());
            warning.setMsg(Constants.convergence.getThWarningDescribe());
            Constants.warningList.add(warning);
//            StartService.timer();
        }
    }

    public static void main(String[] args) {

////        project=projectDao.getProjectById(1);
//        Integer projectInitialLocation=Integer.valueOf((int) project.getInitial().floatValue());
//        Integer location=1;
//        location=projectInitialLocation+location*project.getPrLaserModule();
//        System.out.println(location);

    }
}
